package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.api.Code;
import com.mall.common.api.Result;
import com.mall.entity.PmsBrand;
import com.mall.entity.PmsProduct;
import com.mall.entity.SmsBrand;
import com.mall.entity.SmsNewCommodity;
import com.mall.service.PmsBrandService;
import com.mall.service.SmsBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "品牌推荐")
@RestController
@RequestMapping("/recommendbrands")
public class SmsBrandController {
    @Resource
    private SmsBrandService smsBrandService;

    @Resource
    private PmsBrandService pmsBrandService;

    @ApiOperation(value = "新增品牌推荐")
    @PostMapping
    public Result save(@RequestBody SmsBrand smsBrand) {
        boolean flag = smsBrandService.save(smsBrand);
        return new Result(flag? Code.SAVE_OK: Code.SAVE_ERR,flag);
    }

    @ApiOperation(value = "根据id删除品牌推荐")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = smsBrandService.deleteById(id);
        return new Result(flag?Code.DELETE_OK: Code.DELETE_ERR,flag);
    }

    @ApiOperation(value = "根据id查询品牌推荐")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        SmsBrand smsBrand = new SmsBrand();
        smsBrand = smsBrandService.getById(id);
        Integer code = smsBrand !=null ? Code.GET_OK:Code.GET_ERR;
        String msg = smsBrand !=null ? "successful":"failed,please try again";
        return new Result(smsBrand,code,msg);
    }

    @ApiOperation(value = "查询全部品牌推荐")
    @GetMapping
    public Result getAll(){
        List<SmsBrand> list = smsBrandService.getAll();
        Integer code = list!=null ? Code.GET_OK:Code.GET_ERR;
        String msg = list!=null ? "successful":"failed,please try again";
        return new Result(list,code,msg);
    }

    @PostMapping ("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        boolean flag = smsBrandService.removeByIds(ids);
        return new Result(flag?Code.DELETE_OK: Code.DELETE_ERR,flag);
    }

    @GetMapping("/page")
    public IPage<SmsBrand> getPage(@RequestParam Integer pageNum,
                                   @RequestParam Integer pageSize,
                                   @RequestParam String brandName) {
        IPage<SmsBrand> page = new Page<>(pageNum,pageSize);
        QueryWrapper<SmsBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("brand_name", brandName);
        return smsBrandService.page(page,queryWrapper);
    }

    @ApiOperation(value = "根据改变推荐状态")
    @PutMapping("/{id}")
    public Result update(@PathVariable Integer id) {
        SmsBrand smsBrand = smsBrandService.getById(id);
        if(smsBrand.getRecommendStatus()==1)
        {
            smsBrand.setRecommendStatus(0);
        }else{
            smsBrand.setRecommendStatus(1);
        }
        boolean flag = smsBrandService.save(smsBrand);
        return new Result(flag?Code.UPDATE_OK: Code.UPDATE_ERR,flag);
    }

    @ApiOperation(value = "批量增加品牌推荐")
    @PostMapping("/addlist")
    public Result addList(@RequestBody List<Integer> ids){
        List<SmsBrand> list = new ArrayList<SmsBrand>();
        PmsBrand brand = new PmsBrand();
        for(int i=0;i<ids.size();i++)
        {
            SmsBrand smsBrand = new SmsBrand();
            brand = pmsBrandService.getById(ids.get(i));
            smsBrand.setBrandId(brand.getId());
            smsBrand.setBrandName(brand.getName());
            smsBrand.setRecommendStatus(0);
            smsBrand.setSort(0);
            list.add(smsBrand);
        }
        boolean flag = smsBrandService.saveBatch(list);
        return new Result(flag?Code.SAVE_OK: Code.SAVE_ERR,flag);
    }
}
